package com.zhangmeng.online.exam.controller;

import com.zhangmeng.online.exam.dao.QuestionOptionDao;
import com.zhangmeng.online.exam.dto.Result;
import com.zhangmeng.online.exam.entity.QuestionOption;
import com.zhangmeng.online.exam.entity.User;
import com.zhangmeng.online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/18 11:38
 * @version: 1.0
 */
@RestController
@RequestMapping("/option")
public class QuestionOptionController {

    @Autowired
    private QuestionOptionDao questionOptionDao;

    @RequestMapping("/list")
    public Result list(Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.by(Sort.Direction.DESC, "id"));
        Page<QuestionOption> all = questionOptionDao.findAll(pageable);
        List<QuestionOption> list = all.getContent();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (QuestionOption option : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", option.getId());
            map.put("name", option.getOptionName());
            map.put("optionContent", option.getOptionContent());
            map.put("isAnswer", option.getIsAnswer());
            map.put("addTime", option.getAddTime());
            map.put("explanation", option.getExplanation());
            resultList.add(map);
        }
        return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
    }

    @RequestMapping("/delete")
    public Result delete(Long id) {

        QuestionOption questionOption = questionOptionDao.getById(id);
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return null;
            }
        };


        questionOptionDao.findAll();


        questionOptionDao.deleteById(id);
        return Result.success(null);
    }

    @RequestMapping("/save")
    public Result save(@RequestBody @RequestParam Map<String, Object> params) {

        String id = params.get("id").toString();
        String name = params.get("name").toString();
        String content = params.get("content").toString();
        String isFlag = params.get("isFlag").toString();
        String explanation = params.get("explanation").toString();
        QuestionOption option;
        if (id != null && !id.isEmpty()) {
            option = questionOptionDao.findById(Long.parseLong(id)).get();
        } else {
            option = new QuestionOption();
        }
        option.setOptionName(name);
        option.setOptionContent(content);
        option.setIsAnswer(Boolean.parseBoolean(isFlag));
        option.setExplanation(explanation);
        questionOptionDao.save(option);

        return Result.success(null);
    }


    @RequestMapping("/getQuestionOptionById")
    public Result getQuestionOptionById(Long id) {
        QuestionOption option = questionOptionDao.findById(id).get();
        Map<String, Object> map = new HashMap<>();
        map.put("id", option.getId());
        map.put("name", option.getOptionName());
        map.put("optionContent", option.getOptionContent());
        map.put("isAnswer", option.getIsAnswer());
        map.put("addTime", option.getAddTime());
        map.put("explanation", option.getExplanation());
        return Result.success(map);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody @RequestParam Map<String, Object> params) {
        String id = params.get("id").toString();
        String fieldName = params.get("fieldName").toString();
        String oldValue = params.get("oldValue").toString();
        String newValue = params.get("newValue").toString();

        QuestionOption option = questionOptionDao.findById(Long.parseLong(id)).get();

        /**
         *  map.put("id", option.getId());
         *             map.put("name", option.getOptionName());
         *             map.put("optionContent", option.getOptionContent());
         *             map.put("isAnswer", option.getIsAnswer());
         *             map.put("addTime", option.getAddTime());
         *             map.put("explanation", option.getExplanation());
         */

        switch (fieldName) {
            case "name" -> option.setOptionName(newValue);
            case "optionContent" -> option.setOptionContent(newValue);
            case "isAnswer" -> option.setIsAnswer(Boolean.valueOf(newValue));
            case "explanation" -> option.setExplanation(newValue);
        }

        questionOptionDao.save(option);
        return Result.success(null);
    }
}
